a file upload script that reads the mime type of the file and places it in the appropriate folder. One of my earlier pieces of PHP.

Instructions: Use this file as a stand alone file or you can reference to it in an include ( once you lose the html code and wrap it in a function).
You need to change the define's to point to your appropriate folders, UP_DIR being the base folder, which then has other folder's inside it, (in this case, pics, vids, other).

Add extra folders by defining them, adding the appropriate mime type to $allowed and modifying the inset if statement that starts on line 37.

Note the mime type application/vnd.openxmlformats-officedocument.wordprocessingml.document is the MS Word 2007 .docx file

=======================================================

<?php

	//Make all definitions

	define('UP_DIR', 'C:\localhost\htdocs\Site1\files\\');

	define('MAX_FILE_SIZE', 10240000);

	define('IMAGE', 'pics\\');

	define('VIDEO', 'vids\\');

	define('APP', 'other\\');



	//Initialise flags

	$sizeOK  = false;

	$typeOK  = false;

	$success = false;

	$video   = false;

	$image   = false;

	$app     = false;



	//Create an array of permitted MIME types

	$allowed = array(

		'application/msword', 'application/pdf', 'text/plain', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

		'text/rtf', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'video/avi', 'application/x-zip-compressed', 'audio/mpeg', 

		'text/xml', 'image/bmp');



	if(array_key_exists('upload', $_POST)) {



		//Replace any spaces in the original name for underscores

		$file = str_replace(' ', '_', $_FILES['file']['name']);



		//convert the max size to kb

		$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';



		//Check the file type is permitted

		foreach($allowed as $type) {

			if($type == $_FILES['file']['type']) {

				$typeOK = True;



				//Check to see which folder to place them in

				if(substr($type, 12) == 'msword' || substr($type, 12) == 'pdf' || substr($type, 5) == 'plain' || substr($type, 5) == 'rtf' || substr($type, 5) == 'xml') {

					$app = True;

				}

				elseif(substr($type, 6) == 'gif' || substr($type, 6) == 'jpeg' || substr($type, 6) == 'png' || substr($type, 6) == 'pjpeg' || substr($type, 6) == 'bmp') {

					$image = True;

				}

				elseif(substr($type, 12) == 'vnd.openxmlformats-officedocument.wordprocessingml.document' || substr($type, 12) == 'x-zip-compressed') {

					$app = True;	//Office 2007

				}

				elseif(substr($type, 5) == 'avi') {

					$video = True;

				}



				break;

			}

		}



		//check it isn't too big

		if($_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) {

			$sizeOK = True;

		}



		if($sizeOK && $typeOK) {

			//Lets see what the error level is

			switch($_FILES['file']['error']) {

				case 0:

					//Move file to folder and rename it

					if($app) {

						$success = move_uploaded_file($_FILES['file']['tmp_name'], UP_DIR.APP.$file);

					}

					elseif($image) {

						$success = move_uploaded_file($_FILES['file']['tmp_name'], UP_DIR.IMAGE.$file);

					}

					elseif($video) {

						$success = move_uploaded_file($_FILES['file']['tmp_name'], UP_DIR.VIDEO.$file);

					}



					//Make sure it was moved with no problems

					if($success) {

						$result = "$file uploaded successfully";

					}

					else {

						$result = "Error uploading $file. Please try again.";

					}

					break;

				case 2:	//we ignore error 1 because it is php.ini

					//File was too big!

					$result = "$file was too big to be uploaded. Please try a smaller file";

					break;

				case 4:

					//No file selected

					$result = "No file was selected, please go back and try again";

					break;

				default:

					$result = "System error uploading $file. Please contact a staff member to resolve this issue.";

			}

		}

		else {

			$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: .doc, .txt, .pdf, .jpg, .jpeg, .gof, .rtf, .png";

		}

	}

		//If the form has been submitted, display the result

		if(isset($result)) {

			echo("<p><b>$result</b></p>");

		}

?>

			<form action="" method="post" enctype="multipart/form-data" name="uploadFile" id="uploadFile">

				<label for="file">

					Upload File:

				</label>

				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />

				<input type="file" name="file" id="file" />

				<input type="submit" name="upload" value="upload" />

			</form>